﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Collections.Specialized;
using System.Web;

namespace Windvoice.Library.Mvc
{
    public static class HtmlExtend
    {
        public static MvcHtmlString Pager(this System.Web.Mvc.HtmlHelper helper, int RecordCount, int PageSize = 16)
        {
            int PageIndex = 1;
            if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["page"]) && int.TryParse(HttpContext.Current.Request.QueryString["page"], out PageIndex)) ;
           
            int PageCount = (RecordCount % PageSize == 0) ? RecordCount / PageSize : (RecordCount / PageSize) + 1;
            if (PageIndex <= 0) PageIndex = 1;
            if (PageCount != 0 && PageIndex > PageCount) PageIndex = PageCount;

            NameValueCollection nv = HttpContext.Current.Request.QueryString;
            string ParamString = String.Empty;
            foreach (string key in nv.Keys) if (key != "page")
                    ParamString += key + "=" + nv[key] + "&";
            if (!String.IsNullOrEmpty(ParamString))
                ParamString = ParamString.Substring(0, ParamString.Length - 1);

            #region navigate string builder
            StringBuilder NavigateBuilder = new StringBuilder();
            if (PageCount > 0 && PageIndex != 1)
            {
                NavigateBuilder.Append("<a href=\"?page=1" + (String.IsNullOrEmpty(ParamString) ? "" : "&" + ParamString) + "\" class=\"\">首页</a> ");
                NavigateBuilder.Append("<a href=\"?page=" + (PageIndex - 1) + (String.IsNullOrEmpty(ParamString) ? "" : "&" + ParamString) + "\" class=\"\">前页</a> ");
            }

            if (PageCount > 1)
                for (int i = PageIndex - 5; i < PageIndex + 5; i++)
                {
                    if (i >= 1 && i <= PageCount)
                    {
                        if (i != PageIndex)
                            NavigateBuilder.Append("<a href=\"?page=" + i + "\" class=\"\">" + i + "</a> ");
                        else
                            NavigateBuilder.Append("<b>" + i + "</b> ");
                    }
                }

            if (PageCount > 0 && PageIndex != PageCount)
            {
                NavigateBuilder.Append("<a href=\"?page=" + (PageIndex + 1) + (String.IsNullOrEmpty(ParamString) ? "" : "&" + ParamString) + "\" class=\"\">后页</a> ");
                NavigateBuilder.Append("<a href=\"?page=" + PageCount + (String.IsNullOrEmpty(ParamString) ? "" : "&" + ParamString) + "\" class=\"\">末页</a> ");
            }
            #endregion

            string html = String.Format("<div class=\"page\">{0}</div>", NavigateBuilder.ToString());

            return new MvcHtmlString(html);
        }

        public static MvcHtmlString Pager(this System.Web.Mvc.HtmlHelper helper, int recordCount,string style, int pageSize = 16)
        {
            int PageIndex = 1;
            if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["page"]) && int.TryParse(HttpContext.Current.Request.QueryString["page"], out PageIndex)) ;

            int PageCount = (recordCount % pageSize == 0) ? recordCount / pageSize : (recordCount / pageSize) + 1;
            if (PageIndex <= 0) PageIndex = 1;
            if (PageCount != 0 && PageIndex > PageCount) PageIndex = PageCount;

            NameValueCollection nv = HttpContext.Current.Request.QueryString;
            string ParamString = String.Empty;
            foreach (string key in nv.Keys) if (key != "page")
                    ParamString += key + "=" + nv[key] + "&";
            if (!String.IsNullOrEmpty(ParamString))
                ParamString = ParamString.Substring(0, ParamString.Length - 1);

            /*<div class="manu">
                <span class="disabled"> <  Prev</span>
                <span class="current">1</span>
                <a href="#">2</a>
             * <a href="#">3</a>
             * <a href="#">4</a>
             * <a href="#">5</a>
             * <a href="#">6</a>
             * <a href="#">7</a>
             * ...
             * <a href="#">199</a>
             * <a href="#">200</a>
             * <a href="#">Next  > </a>
             * </div>
             */

            StringBuilder NavigateBuilder = new StringBuilder();
            if (PageCount > 0 && PageIndex != 1)
            {
                NavigateBuilder.Append("<a href=\"?page=1" + (String.IsNullOrEmpty(ParamString) ? "" : "&" + ParamString) + "\" class=\"\">首页</a> ");
                NavigateBuilder.Append("<a href=\"?page=" + (PageIndex - 1) + (String.IsNullOrEmpty(ParamString) ? "" : "&" + ParamString) + "\" class=\"\">前页</a> ");
            }
            else
            {
                NavigateBuilder.Append("<span class=\"disabled\">首页</span> ");
                NavigateBuilder.Append("<span class=\"disabled\">前页</span> ");
            }

            if (PageCount > 1)
                for (int i = PageIndex - 5; i < PageIndex + 5; i++)
                {
                    if (i >= 1 && i <= PageCount)
                    {
                        if (i != PageIndex)
                            NavigateBuilder.Append("<a href=\"?page=" + i + "\" class=\"\">" + i + "</a> ");
                        else
                            NavigateBuilder.Append("<span class=\"current\">" + i + "</span> ");
                    }
                }

            if (PageCount > 0 && PageIndex != PageCount)
            {
                NavigateBuilder.Append("<a href=\"?page=" + (PageIndex + 1) + (String.IsNullOrEmpty(ParamString) ? "" : "&" + ParamString) + "\" class=\"\">后页</a> ");
                NavigateBuilder.Append("<a href=\"?page=" + PageCount + (String.IsNullOrEmpty(ParamString) ? "" : "&" + ParamString) + "\" class=\"\">末页</a> ");
            }
            else
            {
                NavigateBuilder.Append("<span class=\"disabled\">后页</span> ");
                NavigateBuilder.Append("<span class=\"disabled\">末页</span> ");
            }


            string html = String.Format("<div class=\"page\"><div class=\"" + style + "\">{0}</div></div>", NavigateBuilder.ToString());

            return new MvcHtmlString(html);
        }
    }
}
